package leetcode


fun main(args: Array<String>) {
    val a = twoSum(intArrayOf(2, 7, 11, 15), 9)
    a.forEach {
        print(it.toString() + "\t")
    }

}


// https://leetcode.com/problems/two-sum/

//我做了延伸，可以是任意个数相加得到target
fun twoSum(nums: IntArray, target: Int, n: Int = 2): IntArray {
    if (n > nums.size)
        throw IllegalArgumentException("number of n exceed the size of nums")
    //default n =2
    val indiceArray = IntArray(n)


    nums.forEachIndexed { index, i ->
        nums.forEachIndexed { indexInner, iInner ->
            if (index != indexInner && (i + iInner) == target) {
                indiceArray[0] = index
                indiceArray[1] = indexInner
                return indiceArray
            }
        }

    }

    return indiceArray
}


//